package com.jic.member.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.power.MegradeRule;
import com.jic.member.request.MemberGrowthBaseRequest;
import com.jic.member.request.MemberGrowthRuleRequest;
import com.jic.member.request.power.MememberLegalRuleRequest;
import com.jic.member.response.MemberGrowthRuleResponse;
import com.jic.member.response.MememberGrowthBaseResponse;
import com.jic.member.response.power.MegradeRuleResponse;
import com.jic.member.service.MememberGrowthBaseService;
import com.jic.member.service.MememberGrowthRuleService;
import com.jic.member.service.power.MegradeRuleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("msGrowthRule")
public class MemberMsGrowthController {
    @Autowired
    private MememberGrowthRuleService mememberGrowthRuleService;
    @Autowired
    private MememberGrowthBaseService mememberGrowthBaseService;
    @Autowired
    MegradeRuleService megradeRule;

    @ApiOperation("基础列表查询")
    @RequestMapping("queryBaseRule")
    public RestResult<PageResult<MememberGrowthBaseResponse>> queryBaseRule(@RequestBody MemberGrowthBaseRequest memberGrowthBaseRequest){
        return mememberGrowthBaseService.queryBaseRule(memberGrowthBaseRequest);
    }

    @ApiOperation("成长值列表查询")
    @RequestMapping("queryGrowthRule")
    public RestResult<PageResult<MemberGrowthRuleResponse>> queryGrowthRule(@RequestBody MemberGrowthRuleRequest memberGrowthRuleRequest){
        return mememberGrowthRuleService.queryGrowthRule(memberGrowthRuleRequest);
    }

    @ApiOperation("权益列表查询")
    @RequestMapping("queryPowerRule")
    public RestResult<PageResult<MegradeRuleResponse>> queryMegradeRuleList(@RequestBody MememberLegalRuleRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MegradeRule memberRequest = BeanUtil.copy(form, MegradeRule.class);
        memberRequest.setIsMom(2L);
        PageResult<MegradeRule> result = megradeRule.queryPageForLook(memberRequest, page);
        List<MegradeRuleResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MegradeRuleResponse memberResponse = BeanUtil.copy(temp, MegradeRuleResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MegradeRuleResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

}
